package com.jgp.sys.controller;

import com.jgp.common.annotation.UIParam;
import com.jgp.common.pojo.TreeBean;
import com.jgp.sys.model.Menu;
import com.jgp.sys.service.MenuService;
import com.jgp.sys.ui.GridResult;
import com.jgp.sys.ui.Pager;
import com.jgp.sys.ui.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2019-11-01
 */
@CrossOrigin
@RestController
@RequestMapping("/sys/menu-api-ctl")
public class MenuApiController extends JGPBackController {
    
    @Autowired
    private MenuService service;
    
    @RequestMapping("save")
    public Result createMenu(Menu menu) {
        service.save(menu);
        return ajaxRe(true);
    }
    
    @RequestMapping("query-tree")
    public List<TreeBean> queryTree() {
        return service.queryFullTree();
    }
    
    @RequestMapping("query-tree-with-out-platform")
    public List<TreeBean> queryTreeWidthOutPlatform() {
        return service.queryTreeWidthOutPlatform();
    }
    
    @RequestMapping("query-tree-lazy")
    public List<TreeBean> queryTreeLazy(String parentId) {
        return service.queryTreeLazy(parentId,null);
    }
    
    @RequestMapping("query-one")
    public Result queryOne(Long id) {
        Menu menu = service.queryOne(id);
        return ajaxReData("menu", menu);
    }
    
    @RequestMapping("query")
    public Result query(String parentId, String title, String code, String type, @UIParam("pager") Pager pager) {
        List menus = service.query(parentId, title, code, type, pager);
        List<GridResult.Category> categories = new ArrayList<>();
        categories.add(new GridResult.Category("菜单名", "title"));
        categories.add(new GridResult.Category("资源", "link"));
        categories.add(new GridResult.Category("编码", "code"));
        categories.add(new GridResult.Category("图标", "icon"));
        categories.add(new GridResult.Category("是否可用", "enabled"));
        return ajaxReGrid("gdata", categories, menus, pager);
    }
    
    @RequestMapping("remove-menu")
    public Result removeMenu(@RequestBody List<Long> ids) {
        service.removeMenu(ids);
        return ajaxRe(true);
    }
    
    @RequestMapping("refresh-cache")
    public Result refreshCache(){
        service.removeCache();
        service.queryFullTree();
        return ajaxRe(true);
    }
}
